package other;

import java.io.IOException;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import myConn.MyConnection;

/**
 * Servlet implementation class Contact
 */
public class Contact extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String contactNumber = request.getParameter("contactNumber");
		String emailID = request.getParameter("emailID");
		String message = request.getParameter("message");

		MyConnection mc = new MyConnection();
		mc.connect();

		//Validation
		if(name.equals(""))
		{
		 
			request.setAttribute("error", "Please specify you name.");
			request.getRequestDispatcher("contact.jsp").forward(request, response);
			
		}
		
		
		else{
		
		try {

			PreparedStatement ps = mc.con
					.prepareStatement("INSERT INTO contact(name,contactNumber,emailid,message) VALUES(?,?,?,?)");
			ps.setString(1, name);
			ps.setString(2, contactNumber);
			ps.setString(3, emailID);
			ps.setString(4, message);
			ps.execute();

		} catch (Exception e) {
			e.printStackTrace();
		}

		response.sendRedirect("thanks.jsp");
		}
	}

}
